#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "cardCharSelect.h"
#include "cardStageSelect.h"
#include "cardGame.h"
#include "brick.h"

CardCharSelect::CardCharSelect(char gIsSurvival)
{
	/* Bricks */

	//printf("data/heroes\n");

	//Parse file with stats
	FILE *file = fopen("mod/heroes", "r");

	//Failed to open file
	if (!file)
	{
		printf("Error opening file: mod/heroes\n");
		return;
	}

	//Buffer
	char buffer[256];

	//Start parsing
	while (fscanf(file, "%s", buffer) != EOF)
	{
		//Found comment - skip line
		if ( buffer[0] == '#' )
			fscanf(file, "%*[^\n]"); else
		//Found hero
		if ( buffer[0] == '~' )
		{
			//Position on array
			int pos = brick.size();

			//Current brick
			Brick b;

			//Load its graphic
			fscanf(file, "%s", buffer);
			b.load(buffer, ((pos & 1) == 1 ? 1 : 0));

			//Hp
			int hp;
			fscanf(file, "%d", &hp);
			vecHp.push_back(hp);

			//Sp
			/*int sp;
			fscanf(file, "%d", &sp);
			vecSp.push_back(sp);*/

			//Speed and damage
			fscanf(file, "%f", &b.speed);
			//fscanf(file, "%hhu", &b.damage);
			fscanf(file, "%hhu", &b.damage);

			//Position, even numbers to the left
			if ((pos & 1) == 0)
				b.loc.x = -5.0f;//translateSet(-5.0f, -(pos/2) *1.5f, 0.0f);
			//Odd number to the right
			else 
				b.loc.x = 5.0f;//b.translateSet( 5.0f, -(pos/2) *1.5f, 0.0f);

			b.loc.y = -(pos/2) *1.5f;
	
			//Add brick to array
			brick.push_back(b);
		}
	}

	//Close file
	fclose(file);

	/* Bars */

	health.load("obj/stats_board.ply", 0);
	health.loc.y = 3.0f -(0 *2.5f);
	health.loc.z = -5.0f;
	health.max = 255;

	/*
	special.load("obj/stats_board.ply", 0);
	special.loc.y = 4.0f -(1 *2.5f);
	special.loc.z = -5.0f;
	special.max = 255;
	*/

	speed.load("obj/stats_board.ply", 0);
	speed.loc.y = 3.0f -(1 *2.5f);
	speed.loc.z = -5.0f;
	speed.max = 8;
	
	damage.load("obj/stats_board.ply", 0);
	damage.loc.y = 3.0f -(2 *2.5f);
	damage.loc.z = -5.0f;
	damage.max = 255;

	//Start selection from first brick
	prevSel = selection = 0;
	sAnim.max = 5;
	statsToBars();

	//Font
	font.setColorFront(0.9f, 0.327f, 0.689f);
	font.setColorBack (0.26f, 0.121f, 0.256f);
	font.setAlign(1);

	//Data to pass on
	isSurvival = gIsSurvival;
}

void CardCharSelect::input(unsigned char key, bool pressed)
{
	//Do not accept input if animation is running
	if (!anim.isRunning())
	//According to key
	switch (key)
	{
		//Left/right
		case 'a':
		case 'd':
			//Pressed
			if (pressed)
			{
				//Current selection is left
				if ((selection & 1) == 0)
				{
					//There is character on right
					if (brick.size() > 0 && selection < brick.size()-1)
					{
						//Move right and change shown stats
						selection++;
						statsToBars();
					}
				}
				//Current selection is right
				else
				{
					//Move selection leftwards (There is always left)
					selection--;
					statsToBars();
				}
			}
			break;

		//Up
		case 'w':
			if (pressed)
			{
				//Selection is greater/equal to 2 (so it can move upwards)
				if (selection >= 2)
				{
					/* Save previous selection (for animation),
					 * move selection upwards and start animation 
					 */
					prevSel = selection;
					selection -= 2;
					sAnim.startFromStart();
					statsToBars();
				}
			}
			break;
		//Down
		case 's':
			if (pressed)
			{
				/* There are more than 2 bricks (so at least 2 rows)
				 * and selection is not on the last one
				 */
				if (brick.size() > 2 
				&& selection < brick.size()-2) 
				{
					/* Save previous selection (for animation),
					 * move selection downwards and start animation 
					 */
					prevSel = selection;
					selection += 2;
					sAnim.startFromStart();
					statsToBars();
				}
			}
			break;

		//Space
		case '\040':
			//Start animation
			anim.startFromStart();
			break;
	}
}

void CardCharSelect::update()
{
	//Animation updated
	if (anim.update())
		//Animation ended
		if (anim.isAtEnd())
		{
			char isSurv = isSurvival;
			int sel = selection;
			int sel2 = rand() %brick.size();

			//Delete this menu and give selection to next menu
			delete card;
			card = new CardStageSelect(isSurv, sel, sel2);
		}

	//Update selection animation
	sAnim.update();
}

void CardCharSelect::draw()
{
	//Animation's drawing
	Card::draw();

	//Camera movement
	glTranslatef(0.0f, -1.0f, -10.0f);

	//Draw bars
	health .draw();
	//special.draw();
	speed  .draw();
	damage .draw();

	//Texts
	glPushMatrix();
	glTranslatef(health.loc.x -2.0f, health.loc.y, health.loc.z);
	glScalef(1.3f, 1.3f, 1.3f);
	font.draw("hp");
	glPopMatrix();

	/*
	glPushMatrix();
	glTranslatef(special.loc.x -2.0f, special.loc.y, special.loc.z);
	glScalef(1.3f, 1.3f, 1.3f);
	font.draw("sp");
	glPopMatrix();
	*/

	glPushMatrix();
	glTranslatef(speed.loc.x -2.0f, speed.loc.y, speed.loc.z);
	glScalef(1.3f, 1.3f, 1.3f);
	font.draw("spd");
	glPopMatrix();

	glPushMatrix();
	glTranslatef(damage.loc.x -2.0f, damage.loc.y, damage.loc.z);
	glScalef(1.3f, 1.3f, 1.3f);
	font.draw("dmg");
	glPopMatrix();

	//More camera movement (so it doesn't affect bars and text)
	if (sAnim.isRunning())
		glTranslatef(0,	-(brick[prevSel].loc.y 
			+sAnim.interpolate(brick[selection].loc.y -brick[prevSel].loc.y)), 0);
		//glTranslatef(0,	-(brick[prevSel].y() 
		//	+sAnim.interpolate(brick[selection].y() -brick[prevSel].y())), 0);
	else
		glTranslatef(0,	-brick[selection].loc.y, 0);
		//glTranslatef(0,	-brick[selection].y(), 0);
	glRotatef(15.0f, 1.0f, 0.0f, 0.0f);

	//Draw bricks
	unsigned int i;
	for (i = 0; i < brick.size(); i++)
	{
		//Selected brick, quake effect
		if (selection == i)
		{
			//Seperate X and Y
			float tempX = ((rand() +1) %100) /1000.0f;
			float tempY = ((rand() +1) %100) /1000.0f;

			//Apply offset
			//brick[i].translateAdd(tempX, tempY, 0.0f);
			brick[i].loc.x += tempX;
			brick[i].loc.y += tempY;

			//Draw choice
			brick[i].draw();

			//Cancel offset
			//brick[i].translateAdd(-tempX, -tempY, 0.0f);
			brick[i].loc.x -= tempX;
			brick[i].loc.y -= tempY;
		}
		//Other non selected bricks
		else brick[i].draw();
	}
}

//Apply current selected hero's stats to bars
void CardCharSelect::statsToBars()
{
	//Health
	health.cur = vecHp[selection];
	//special.cur = vecSp[selection];
	speed.cur = brick[selection].speed *10;
	if (brick[selection].damage < 60)
		damage.cur = brick[selection].damage *255 /60;
	else
		damage.cur = 255;
}
